<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: Cache.class.php 2702 2012-02-02 12:35:01Z liu21st $

/**
 +------------------------------------------------------------------------------
 * 缓存管理类
 +------------------------------------------------------------------------------
 * @category   Think
 * @package  Think
 * @subpackage  Util
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id: Cache.class.php 2702 2012-02-02 12:35:01Z liu21st $
 * @lastmodify 2012-12-12 09:31:07 by mashanling
 +------------------------------------------------------------------------------
 */
class Cache {
    protected $connected;//是否连接
    protected $handler;//操作句柄
    protected $options = array();//缓存连接参数

    /**
     * 取得缓存类实例
     *
     * @lastmodify 2012-12-12 09:43:10 by mashanling
     *
     * @static
     * @access public
     *
     * @return object 缓存类实例
     */
    static public function getInstance() {
        return get_instance_of(__CLASS__, 'connect', func_get_args());
    }

    /**
     * 队列缓存
     *
     * @lastmodify 2012-12-12 09:48:44 by mashanling
     *
     * @access protected
     *
     * @return mixed 缓存对应方法返回结果
     */
    protected function queue($key) {
        static $_handler = array(
            'file'   => array('F', 'F'),
            'xcache' => array('xcache_get', 'xcache_set'),
            'apc'    => array('apc_fetch', 'apc_store'),
        );

        $queue = isset($this->options['queue']) ? $this->options['queue'] : 'file';
        $fun   = $_handler[$queue];
        $value = $fun[0]('think_queue');
        $value = $value ? $value : array();

        array_push($value, $key);//进列

        if(count($value) > $this->options['length']) {
            $key = array_shift($value);//出列
            $this->rm($key);// 删除缓存
        }

        return $fun[1]('think_queue', $value);
    }

    /**
     * 魔术方法__get
     *
     * @lastmodify 2012-12-12 09:37:49 by mashanling
     *
     * @access public
     *
     * @param string $name 获取对象名称
     *
     * @return mixed $this->get()返回结果
     */
    public function __get($name) {
        return $this->get($name);
    }

    /**
     * 魔术方法__set
     *
     * @lastmodify 2012-12-12 09:39:06 by mashanling
     *
     * @access public
     *
     * @param string $name  设置对象名称
     * @param mixed  $value 设置值
     *
     * @return mixed $this->set()返回结果
     */
    public function __set($name, $value) {
        return $this->set($name, $value);
    }

    /**
     * 魔术方法__unset
     *
     * @lastmodify 2012-12-12 09:40:01 by mashanling
     *
     * @access public
     *
     * @param string $name 销毁对象名称
     *
     * @return void 无返回值
     */
    public function __unset($name) {
        $this->rm($name);
    }

    /**
     * 连接缓存
     *
     * @lastmodify 2012-12-12 09:31:07 by mashanling
     *
     * @access public
     * @throws ThinkExecption
     *
     * @param string $type   缓存类型
     * @param array $options 配置数组
     *
     * @return object 缓存实例
     */
    public function connect($type = '', $options = array()) {
        $type  = $type ? $type : C('DATA_CACHE_TYPE');
        $type  = strtolower(trim($type));
        $class = 'Cache' . ucwords($type);

        if(is_file(CORE_PATH . ($v = 'Driver/Cache/' . $class . '.class.php'))) {//内置驱动
            $path = CORE_PATH;
        }
        else{//扩展驱动
            $path = EXTEND_PATH;
        }

        if(require_cache($path . $v)) {
            $cache = new $class($options);
        }
        else {
            throw new Exception(L('_CACHE_TYPE_INVALID_') . ':' . $type);
        }

        return $cache;
    }

    /**
     * 获取配置值
     *
     * @lastmodify 2012-12-12 09:43:10 by mashanling
     *
     * @access public
     *
     * @param string $name  配置名称
     *
     * @return mixed 配置值
     */
    public function getOptions($name) {
        return $this->options[$name];
    }

    /**
     * 设置配置
     *
     * @lastmodify 2012-12-12 09:41:02 by mashanling
     *
     * @access public
     *
     * @param string $name  配置名称
     * @param mixed  $value 配置值
     *
     * @return void 无返回值
     */
    public function setOptions($name, $value) {
        $this->options[$name] = $value;
    }
}